package com.eastcode.mobile.client.action;

import java.util.Iterator;
import java.util.Map;
import java.util.logging.Logger;

import com.eastcode.base.action.BaseAction;
import com.eastcode.base.util.DateUtil;
import com.eastcode.base.util.HttpUtil;
import com.eastcode.mobile.client.service.ReceiveService;
import com.eastcode.mobile.server.user.entity.CallLog;
import com.eastcode.mobile.server.user.entity.Contact;
import com.eastcode.mobile.server.user.entity.Sms;
import com.eastcode.mobile.server.util.ServerLocator;

public class ReceivAction extends BaseAction {

	private static final long serialVersionUID = 1093903102967232262L;

	private ReceiveService receiveService = ServerLocator.getReceiveService();
	Logger log = Logger.getLogger(this.getClass().getName());

	@Override
	protected void execute() {
		prepare(this);
	}

	/**
	 * 
	 * 数据格式：{id=6, number=125487, date=2011-09-12 10:57:59, type=2, name=null}
	 */
	public void callLog() {
		Map<String, String> paramMap = HttpUtil.parseRequest(request);
		Iterator<String> iterator = paramMap.keySet().iterator();

		while (iterator.hasNext()) {
			String key = iterator.next();
			String param = request.getParameter(key);

			param = param.substring(1, param.length() - 1);
			String[] values = param.split(",");

			CallLog callLog = new CallLog();
			boolean hasValue = false;
			// 拆分字符串
			for (int i = 0; values != null && i < values.length; i++) {

				String[] result = values[i].split("=");
				if (result.length <= 1) {
					continue;
				}
				String name = result[0].trim();
				String value = result[1].trim().replaceAll("null", "");
				if (name.equals("number")) {
					callLog.setCallNumber(value);
				} else if (name.equals("name")) {
					callLog.setCallName(value);
				} else if (name.equals("type")) {
					callLog.setType(value);
				} else if (name.equals("id")) {
					callLog.setClientKey(value);
				} else if (name.equals("date")) {
					callLog.setCallDate(value);
				} else if (name.equals("USER_ID")) {
					callLog.setUserId(value);
				}
				hasValue = true;
			}

			// 保存通话记录
			if (hasValue) {
				receiveService.save(callLog);
			}
		}
	}

	/**
	 * 通讯录 数据格式：{id=4, number=125487, date=2011-09-12 10:56:48, type=2,
	 * name=null}
	 * 
	 */
	public void contact() {

		Map<String, String> paramMap = HttpUtil.parseRequest(request);
		Iterator<String> iterator = paramMap.keySet().iterator();

		while (iterator.hasNext()) {

			Contact contact = new Contact();
			boolean hasValue = false;

			contact.setCreateTime(DateUtil.getToday());

			String key = iterator.next();
			String param = paramMap.get(key);
			param = param.substring(1, param.length() - 1);
			String[] values = param.split(",");

			// 拆分字符串
			for (int i = 0; values != null && i < values.length; i++) {

				String[] result = values[i].split("=");
				if (result.length <= 1) {
					continue;
				}
				String name = result[0].trim();
				String value = result[1].trim().replaceAll("null", "");
				if (name.equals("contactId")) {
					contact.setClientKey(value);
				} else if (name.equals("name")) {
					contact.setName(value);
				} else if (name.equals("number")) {
					contact.setNumber(value);
				} else if (name.equals("USER_ID")) {
					contact.setUserId(value);
				} else if (name.equals("date")) {
					contact.setCreateTime(DateUtil.parseDateTime(value));
				}

				hasValue = true;
			}

			if (hasValue) {
				// 保存通讯录
				receiveService.save(contact);
			}
		}

	}

	/**
	 * 保存短信
	 * 
	 */
	public void sms() {
		Map<String, String> paramMap = HttpUtil.parseRequest(request);
		Iterator<String> iterator = paramMap.keySet().iterator();

		while (iterator.hasNext()) {

			Sms sms = new Sms();
			boolean hasValue = false;

			String key = iterator.next();
			String param = paramMap.get(key);
			param = param.substring(1, param.length() - 1);
			String[] values = param.split(",");

			sms.setClientKey(key);
			// 拆分字符串
			for (int i = 0; values != null && i < values.length; i++) {

				String[] result = values[i].split("=");
				if (result.length <= 1) {
					continue;
				}
				String name = result[0].trim();
				String value = result[1].trim().replaceAll("null", "");

				if (name.equals("name")) {
					sms.setSender(value);
					sms.setReceiver(value);
				} else if (name.equals("content")) {
					sms.setMessage(value);
				} else if (name.equals("phoneNumber")) {
					sms.setSenderNumber(value);
					sms.setReceiverNumber(value);
				} else if (name.equals("USER_ID")) {
					sms.setUserId(value);
				} else if (name.equals("date")) {
					sms.setCreateTime(DateUtil.parseDateTime(value));
				} else if (name.equals("type")) {
					if (Integer.parseInt(value) == 1) {
						sms.setSender("");
						sms.setSenderNumber("");
					} else if (Integer.parseInt(value) == 2) {
						sms.setReceiver("");
						sms.setReceiverNumber("");
					} else {
						sms.setSender("");
						sms.setSenderNumber("");
						sms.setReceiver("");
						sms.setReceiverNumber("");
					}
					sms.setType(value);
				}

				hasValue = true;
			}

			if (hasValue) {
				// 保存通讯录
				receiveService.save(sms);
			}
		}

	}
}
